package com.service.impl;

import com.entity.Category;
import com.github.pagehelper.PageInfo;
import com.mapper.BookMapper;
import com.mapper.CategoryMapper;
import com.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("categoryService")
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BookMapper bookMapper;
    @Override
    public void insertCategory(Category category) {
        categoryMapper.insertCategory(category);
    }

    @Override
    public void deleteCategoryById(Integer id) {
        int i = categoryMapper.countRowsByParentId(id);
        if(i > 0){
            throw new RuntimeException("该类别已有子类别，不能删除");
        }
        if(bookMapper.countRowsByCategoryId(id) > 0){
            throw new RuntimeException("该类别下已有图书，不能删除");
        }
        categoryMapper.deleteCategoryById(id);
    }

    @Override
    public void updateCategory(Category category) {
        categoryMapper.updateCategory(category);
    }

    @Override
    public Category selectCategoryById(Integer id) {
        return categoryMapper.selectCategoryById(id);
    }

    @Override
    public Category selectCategoryByName(String name) {
        return categoryMapper.selectCategoryByName(name);
    }

    @Override
    public List<Category> selectCategory() {
        return categoryMapper.selectCategory();
    }

    @Override
    public PageInfo<Category> selectPageCategory(Integer pageNum, Integer pageSize) {
        return new PageInfo<>(categoryMapper.selectPageCategory(pageNum,pageSize));
    }

    @Override
    public List<Category> selectCategoriesByLevels(Integer levels) {
        return categoryMapper.selectCategoriesByLevels(levels);
    }

    @Override
    public List<Category> selectCategoryByChildren() {
        return categoryMapper.selectCategoryByChild();
    }

    @Override
    public List<Category> showCategoriesByParentId(Integer parent_id) {
        return categoryMapper.selectCategoriesByParentId(parent_id);
    }
}
